package ar.edu.utn.frsf.cidisi.upcolbpip.upgen.util;

import java.util.Vector;

import ar.edu.utn.frsf.cidisi.upcolbpip.UPColBPIPModel;
import ar.edu.utn.frsf.cidisi.upcolbpip.UpcolbpipFactory;
import ar.edu.utn.frsf.cidisi.upcolbpip.b2bcollaborations.B2BCollaboration;
import ar.edu.utn.frsf.cidisi.upcolbpip.b2bcollaborations.B2bcollaborationsFactory;
import ar.edu.utn.frsf.cidisi.upcolbpip.collaborativebusinessprocesses.CollaborativeBusinessProcess;
import ar.edu.utn.frsf.cidisi.upcolbpip.collaborativebusinessprocesses.CollaborativebusinessprocessesFactory;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.And;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.BusinessMessage;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.ControlFlowSegment;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.InteractionPath;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.InteractionProtocol;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.InteractionProtocolElement;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.InteractionprotocolsFactory;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.Termination;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.Xor;

public class CreateNTree {

	
	private NTree tree;
	private Vector<String> protocol;
	private int i=0;
	private int idNodo = 0;
	
	public CreateNTree() {
		tree = new NTree();
	}
	
	public void addInteractionProtocols(Vector< Vector<String> > newProtocols) {
		
		for(int j=0;j<newProtocols.size();j++) {
			i=0;
			addNewNode(newProtocols.get(j),tree,"");
			
		}
		
	}
	
	public NTree getTree() {
		return tree;
	}
	
	public NTree ocurrenceOf(NTree aux,String nombre){
		
		for(int i=0;i<aux.getSons().size();i++) {
			if(aux.getSons().get(i).getElementName().equals(nombre)) 
				return aux.getSons().get(i);
		}
		return null;
	}
	
	public void addNewNode(Vector<String> newProtocol,NTree padre,String nodoPadre) {

		String aux=nodoPadre, anterior=""; int cont=0;
		protocol = newProtocol; boolean included =false;
		idNodo++;
		NTree nuevohijo = new NTree(padre,idNodo);
		
		
		for(int k=i;k<protocol.size();k++) {
			if(protocol.get(k) == "And")  {
				aux += "And||";
			}
			else if(protocol.get(k) == "Termination")  {
				aux += "Termination||";
			}
			else if(protocol.get(k) == "Xor")  {
				aux += "Xor||";
			}
			else if(protocol.get(k) == "Child")  {
				cont++;k++;
				for(;(k+1)<protocol.size() && cont !=0/* && protocol.get(k+1)!="Child"*/;k++) {
					if(protocol.get(k)=="Child") cont++;
					else if(protocol.get(k)=="ChildOut") cont--;
				}
				k--;
				cont = 0;
			}
			else if(protocol.get(k) == "ChildOut")  {
				if((k+1)==protocol.size()) break;
				else if(protocol.get(k+1) == "Child") k++;
				else break;
			}
		}
			
		
		aux = aux.substring(0, aux.length()-2);
		if(ocurrenceOf(padre,aux)==null) {
			nuevohijo.setElementName(aux);
			included = false;
		}
		else {
			nuevohijo = ocurrenceOf(padre,aux);
			idNodo--;
			included = true;
		}
		
		
		for(;i<protocol.size();i++) {
			if(protocol.get(i) == "And")  {
				anterior = new String("And");
			}
			else if(protocol.get(i) == "Termination")  {
				anterior = new String("Termination");
			}
			else if(protocol.get(i) == "Xor")  {
				anterior = new String("Xor");
			}
			else if(protocol.get(i) == "Child")  {
				i++;
				addNewNode(newProtocol,nuevohijo,anterior+"->");
				
			}
			else if(protocol.get(i) == "ChildOut")  {
				if((i+1)==protocol.size()||protocol.get(i+1) != "Child") break;
				else if(protocol.get(i+1) == "Child") i++;
			}
		}
		if(!included) padre.getSons().add(nuevohijo);
	//	i++;
		
	}
}
